import React from 'react';
import {Table} from 'antd';
import PropTypes from 'prop-types';
import {formatDateTime} from '../utils';
import {TABLE, PAGINATION_OPTIONS, COMMON_TABLE_QUERY_FAIL_TEXT, COMMON_TABLE_EMPTY_TEXT, DATETIME_FORMAT} from '../constants';
import Ellipsis from 'ant-design-pro/lib/Ellipsis';

export class TablePanel extends React.PureComponent {
    constructor(props) {
        super(props);
        this.onChangeSort = this.onChangeSort.bind(this);
        this.state = {
            loading: false
        };
    }
    onChangeSort = (pagination, filters, sorter) => {
        this.props.onChangeSort({
            pageIndex: pagination.current - 1,
            pageSize: pagination.pageSize,
            sortedColumnKey: sorter.columnKey,
            sortedOrder: sorter.order,
        });
    };
    render() {
        const {formatMessage} = this.props.intl;
        const {pageIndex, pageSize, total} = this.props;
        const locale = {
            emptyText: this.props.hasError ? COMMON_TABLE_QUERY_FAIL_TEXT : COMMON_TABLE_EMPTY_TEXT
        };
        const pagination = {
            total,
            pageSize,
            current: pageIndex + 1,
            ...PAGINATION_OPTIONS
        };

        const columns = [{
            title: formatMessage({
                id: 'tablePanel.column.brandCode',
                defaultMessage: '品牌编号'
            }),
            dataIndex: 'brandCode',
            sorter: true,
            sortOrder: this.props.sortedColumnKey === 'brandCode' && this.props.sortedOrder
        }, {
            title: formatMessage({
                id: 'tablePanel.column.dealerCode',
                defaultMessage: '总代号',
            }),
            dataIndex: 'dealerCode',
            sorter: true,
            sortOrder: this.props.sortedColumnKey === 'dealerCode' && this.props.sortedOrder
        }, {
            title: formatMessage({
                id: 'tablePanel.column.prtDataType',
                defaultMessage: '单据类型'
            }),
            dataIndex: 'prtDataType',
            sorter: true,
            sortOrder: this.props.sortedColumnKey === 'prtDataType' && this.props.sortedOrder
        }, {
            title: formatMessage({
                id: 'tablePanel.column.code',
                defaultMessage: '单据号'
            }),
            dataIndex: 'code',
            sorter: true,
            sortOrder: this.props.sortedColumnKey === 'code' && this.props.sortedOrder,
        }, {
            title: formatMessage({
                id: 'tablePanel.column.oldStatus',
                defaultMessage: '原状态'
            }),
            dataIndex: 'oldStatus',
            sorter: true,
            sortOrder: this.props.sortedColumnKey === 'oldStatus' && this.props.sortedOrder,
        }, {
            title: formatMessage({
                id: 'tablePanel.column.nowStatus',
                defaultMessage: '新状态'
            }),
            dataIndex: 'nowStatus',
            sorter: true,
            sortOrder: this.props.sortedColumnKey === 'nowStatus' && this.props.sortedOrder
        }, {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.confirmerName',
                defaultMessage: '操作人姓名',
            }),
            dataIndex: 'confirmerName'
        }, {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.confirmTime',
                defaultMessage: '操作日期',
            }),
            dataIndex: 'confirmTime',
            render: text => formatDateTime(text, DATETIME_FORMAT)
        }, {
            title: this.props.intl.formatMessage({
                id: 'tablePanel.column.content',
                defaultMessage: '修改内容',
            }),
            dataIndex: 'content',
            render: text => <Ellipsis tooltip fullWidthRecognition length={40}>{text}</Ellipsis>
        }, {
            title: formatMessage({
                id: 'tablePanel.column.remark',
                defaultMessage: '备注'
            }),
            dataIndex: 'remark',
            render: text => <Ellipsis tooltip fullWidthRecognition length={30}>{text}</Ellipsis>
        }];
        return (
            <Table
                className={'white-space-nowrap'}
                columns={columns}
                dataSource={this.props.data}
                loading={this.props.loading || this.state.loading}
                onChange={this.onChangeSort}
                pagination={pagination}
                rowKey="id"
                locale={locale}
                {...TABLE}/>
        );
    }
}
TablePanel.propTypes = {
    data: PropTypes.array.isRequired,
    hasError: PropTypes.string.isRequired,
    loading: PropTypes.bool.isRequired,
    refreshListData: PropTypes.func.isRequired,
    total: PropTypes.number.isRequired,
    onChangeSort: PropTypes.func.isRequired,
    intl: PropTypes.object,
    pageIndex: PropTypes.number,
    pageSize: PropTypes.number,
    sortedColumnKey: PropTypes.string,
    sortedOrder: PropTypes.string,
};

import {connect} from 'react-redux';
import {tableSearch} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {injectIntl} from './intl';

const getData = selectorFactory(['page', 'domainData', 'list', 'data']);
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const mapStateToProps = state => ({
    data: getData(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    hasError: state.getIn(['page', 'domainData', 'list', 'message']),
    total: state.getIn(['page', 'domainData', 'list', 'total']),
    pageIndex: state.getIn(['page', 'appState', 'queryBySort', 'pageIndex']),
    pageSize: state.getIn(['page', 'appState', 'queryBySort', 'pageSize']),
    permissions: getPermissions(state),
    sortedOrder: state.getIn(['page', 'appState', 'queryBySort', 'sortedOrder']),
    sortedColumnKey: state.getIn(['page', 'appState', 'queryBySort', 'sortedColumnKey'])
});

const mapDispatchToProps = dispatch => ({
    onChangeSort: option => dispatch(tableSearch(option)),
    refreshListData: () => dispatch(tableSearch()),
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(TablePanel));


